var express = require('express');
require('express-async-errors');
var router = express.Router();

var { pool, model, connection } = require(__dirname + '/libs/model');

/*
CREATE TABLE IF NOT EXISTS `test` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(100) COLLATE utf8mb4_bin NOT NULL,
  `content` varchar(300) COLLATE utf8mb4_bin NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=4 ;
*/

async function start() {
  //test log_error
  //await model.update("update test set title=? where c=?", [1, 2]);
  /*
  //select
  let articles = await model.select("select itemid,title from article order by itemid desc limit 10");
  console.log(articles);
  //find
  let article = await model.find("select * from article where id=? limit 1", [14]);
  console.log(article);
  //column
  let article_title_array = await model.column("select title from article order by itemid desc limit 5");
  console.log(article_title_array);
  //value
  let title = await model.value("select title from article where itemid=? limit 1", [2]);
  console.log(title);
  //nickname_map
  let title_map = await model.map("select itemid,title from article order by rand() limit 3");
  console.log(title_map);
  //insert
  let insertid = await model.insert("insert into test(title,content) values (?,?)", ['你好' + Math.random(), '世界' + Math.random()]);
  console.log(insertid);
  //update
  let r = await model.update("update test set title=? where id=?", ['hello', 1]);
  console.log(r);
  //delete
  let r2 = await model.delete("delete from test where id=?", [2]);
  console.log(r2);

  let a = await model.i("insert into test set ?", { title: 8, content: 8, demo: 'a' });
  console.log(a);
  let b = await model.u("update test set ? where id=?", { title: 99 }, [5]);
  console.log(b);
  */
  let multi = model.multi(100);
  multi.push("select title from article limit ?", [3], 'select');
  multi.push("select id from test limit ?", [2], 'find');
  multi.push("select itemid from time limit ?", [2], 'column');
  multi.push("select title from article limit ?", [1], 'value');
  multi.push("select itemid,title from article limit ?", [3], 'map');
  let [a, b, c, d, e] = await multi.exec();
  console.log('select', a);
  console.log('find', b);
  console.log('column', c);
  console.log('value', d);
  console.log('map', e);

  connection.end(function (err) {
    console.log('connection closed');
    process.exit();
  });
}



start();
